package com.qiaofang.applet.config;

import com.qiaofang.applet.filter.AuthFilter;
import com.qiaofang.applet.filter.RefreshTokenFilter;
import com.qiaofang.applet.filter.WebsiteFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author QF2019
 */
@Configuration
public class FilterConfig {


    @Autowired
    private AuthFilter authFilter;

    @Autowired
    private WebsiteFilter websiteFilter;

    @Autowired
    private RefreshTokenFilter refreshTokenFilter;

    /**
     * 通过域名初始化QfContext上下文Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean registWebsiteFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(websiteFilter);
        registration.addUrlPatterns("/api/*");
        registration.setName("websiteFilter");
        // 值越小，Filter越靠前
        registration.setOrder(2);
        return registration;
    }

    /**
     * Token验证 Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean registerAuthFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(authFilter);
        registration.addUrlPatterns("/api/pcweb/auth/*");
        registration.setName("authFilter");
        // 值越小，Filter越靠前
        registration.setOrder(3);
        return registration;
    }

    /**
     * 刷新token Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean registerRefreshTokenFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(refreshTokenFilter);
        registration.addUrlPatterns("/api/*");
        registration.setName("refreshTokenFilter");
        // 值越小，Filter越靠前
        registration.setOrder(99);
        return registration;
    }
}
